﻿using Managistics.Warehouse.Configuration;
using Managistics.Warehouse.Controllers.Interfaces;
using Managistics.Warehouse.Views.Interfaces;
using Managistics.Warehouse.Factories;
using Managistics.Core.Services;
using System.Collections.Generic;
using Managistics.Model;
using Managistics.Model.ValueObject;

namespace Managistics.Warehouse.Controllers
{
    public class PurchaseController : IPurchaseController
    {
        /// <summary>
        /// Local instance of the configuration class that we'll end up binding to
        /// </summary>
        public ApplicationConfiguration appConfig = null;

        readonly IViewEnterFromPurchaseView fromPurchaseView;
        readonly IMainView mainView;
        //readonly ComplexParameterObject parameter;

        private readonly IPurchaseOrderService purchaseOrderService;
        private PurchaseOrderVO vo;

        public PurchaseController(IViewEnterFromPurchaseView fromPurchaseView, IMainView view,
            IPurchaseOrderService purchaseOrderService)
        {
            this.fromPurchaseView = fromPurchaseView;
            this.mainView = view;
            this.purchaseOrderService = purchaseOrderService;

            // Simply assign the default config object - it gets loaded via 
            // the default constructor defined in AppConfig.cs
            this.appConfig = GlobalApp.Configuration;
            this.LoadPurchaseOrders();

            fromPurchaseView.LoadPurchaseOrders += LoadPurchaseOrders;
                        
        }

        public void Run()
        {
            //fromPurchaseView.Open(this.mainView); muon show o dau
            fromPurchaseView.Open();
        }

        public void Stop(string key) { }

        private void LoadPurchaseOrders()
        {
            this.vo = fromPurchaseView.VOView2Model();
            if (this.vo != null)
            {
                this.vo.purchaseOrderList = purchaseOrderService.GetPurchaseOrder(this.vo);
                fromPurchaseView.VOModel2View(this.vo);
            }
        }
        
    }
}